import pg from 'pg';
const { Pool } = pg;

// 配置数据库连接池
const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
  max: 20, // 最大连接数
  idleTimeoutMillis: 30000, // 空闲连接30秒后关闭
  connectionTimeoutMillis: 2000, // 连接超时时间
  ssl: process.env.NODE_ENV === 'production' ? {
    rejectUnauthorized: false
  } : false
});

// 连接池事件监听
pool.on('connect', () => {
  console.log('数据库连接成功');
});

pool.on('error', (err) => {
  console.error('数据库连接错误:', err);
});

// 封装查询方法
const query = async (text, params, client) => {
  const start = Date.now();
  const executionId = Math.random().toString(36).substring(2, 8);
  
  try {
    const executor = client || pool;
    const res = await executor.query(text, params);
    const duration = Date.now() - start;
    
    if (process.env.NODE_ENV === 'development') {
      console.log(`[SQL ${executionId}]`, {
        query: text,
        params,
        duration: `${duration}ms`,
        rows: res.rowCount
      });
    }
    
    return res;
  } catch (err) {
    console.error(`[SQL ${executionId} 错误]`, {
      query: text,
      params,
      error: err.message
    });
    throw err;
  }
};

// 事务执行方法
const transaction = async (callback) => {
  const client = await pool.connect();
  try {
    await client.query('BEGIN');
    const result = await callback({
      query: (text, params) => query(text, params, client)
    });
    await client.query('COMMIT');
    return result;
  } catch (err) {
    await client.query('ROLLBACK');
    throw err;
  } finally {
    client.release();
  }
};

// 获取可用时间段
const getAvailableTimeSlots = async (date) => {
  const res = await query(
    `SELECT time_slot FROM appointments 
     WHERE appointment_date = $1 ORDER BY time_slot`,
    [date]
  );
  return res.rows.map(row => row.time_slot);
};

// 创建预约
const createAppointment = async (appointment) => {
  const { name, phone, wechat, email, date, timeSlot, notes } = appointment;
  
  return transaction(async (tx) => {
    // 检查时间段是否可用
    const existing = await tx.query(
      'SELECT 1 FROM appointments WHERE appointment_date = $1 AND time_slot = $2',
      [date, timeSlot]
    );
    
    if (existing.rows.length > 0) {
      throw new Error('Time slot already booked');
    }
    
    // 创建预约
    const res = await tx.query(
      `INSERT INTO appointments 
       (name, phone, wechat, email, appointment_date, time_slot, notes)
       VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *`,
      [name, phone, wechat, email, date, timeSlot, notes]
    );
    
    return res.rows[0];
  });
};

export { pool, query, transaction, getAvailableTimeSlots, createAppointment };

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
    [name, phone, wechat, email, appointment_date, time_slot, notes]
  );
  
  return rows[0];
}

export async function getAvailableTimeSlots(date) {
  const { rows } = await query(
    'SELECT time_slot FROM appointments WHERE appointment_date = $1',
    [date]
  );
  return rows.map(row => row.time_slot);
}

export async function createAppointment(appointment) {
  const { name, phone, wechat, email, appointment_date, time_slot, notes } = appointment;
  
  const { rows } = await query(
    'INSERT INTO appointments (name, phone, wechat, email, appointment_date, time_slot, notes) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',